package com.weavernorth.humanresource;

import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;
import java.util.Iterator;

/**
 * Created by guo_zy on 2020/1/2.
 * 入职定薪
 */
public class personnelEntrySalary implements Action {
    //    public static Log log = LogFactory.getLog(personnelEntrySalary.class.getName());
//public Logger log = Logger4jUtil.getLoggerByName("/action/EntryAction/");
    @Override
    public String execute(RequestInfo requestInfo) {
        Logger log = Logger4jUtil.getLoggerByName("/action/EntryAction/");
        log.info("入职定薪接口日志开始=======>");
        String fhz = "";
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        //提示信息
        String strMessage = "";
        String requestid = requestInfo.getRequestid();//请求ID
        //String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        String hth = "";//员工编号
        String sxrq = "";//生效日期
        String hblx = "";//货币类型
        String sqyy = "";//申请原因
        String xzlx = "";//薪资类型
        String xzqy = "";//薪资区域
        String xzgjb = "";//薪资岗级别
        String xzgdc = "";//薪资岗档次
        String jbgz = "";//基本工资
        String jxgz = "";//绩效工资
        String nzjjjs = "";//年终奖金基数
        BigDecimal b_dqxssq = new BigDecimal("0.0");//地区系数（申请）
        BigDecimal b_xyznxssq = new BigDecimal("0.0");//行业职能系数（申请）
        BigDecimal b_dqbtsq = new BigDecimal("0.0");//地区补贴（申请）
        BigDecimal b_zxbl = new BigDecimal("0.0");//试用期执行比例（申请）
//        String gzfl="";//津贴类型
//        String je="";//金额
        String zbid = "";//主表ID

        String sql = "select hth,sxrq,hblx,sqyy,xzlx,xzqy,xzgjb,xzgdc,jbgz,jxgz,nzjjjs,dqxssq,xyznxssq,dqbtsq,syqzxbl  from " + tablename + " where requestid =" + requestid;
        rs.execute(sql);
        if (rs.getCounts() != 1)
            return FAILURE_AND_CONTINUE;
        while (rs.next()) {
            zbid = rs.getString("zbid");
            hth = rs.getString("hth");
            sxrq = rs.getString("sxrq");
            hblx = rs.getString("hblx");
            sqyy = "01";
            rs.getString("sqyy");
            xzlx = getxzlx(rs.getString("xzlx"));
            xzqy = getxzqy(rs.getString("xzqy"));
            xzgjb = rs.getString("xzgjb");
            xzgdc = rs.getString("xzgdc");
            jbgz = rs.getString("jbgz");
            if ("".equals(jbgz)) {
                jbgz = "0.00";
            }
            jxgz = rs.getString("jxgz");
            if ("".equals(jxgz)) {
                jxgz = "0.00";
            }
            nzjjjs = rs.getString("nzjjjs");
            if ("".equals(nzjjjs)) {
                nzjjjs = "0.00";
            }
            BigDecimal dqxssq = new BigDecimal(Util.null2s(rs.getString("dqxssq"), "0.0"));
            b_dqxssq = b_dqxssq.add(dqxssq);
            log.info("地区系数（申请）：" + b_dqxssq);
            BigDecimal xyznxssq = new BigDecimal(Util.null2s(rs.getString("xyznxssq"), "0.0"));
            b_xyznxssq = b_xyznxssq.add(xyznxssq);
            log.info("行业职能系数（申请）：" + b_xyznxssq);
            BigDecimal dqbtsq = new BigDecimal(Util.null2s(rs.getString("dqbtsq"), "0.0"));
            b_dqbtsq = b_dqbtsq.add(dqbtsq);
            log.info("地区补贴（申请）：" + b_dqbtsq);

            BigDecimal syqzxbl = new BigDecimal(Util.null2s(rs.getString("syqzxbl"), "0.0"));
            b_zxbl = b_zxbl.add(syqzxbl);
            log.info("试用期执行比例：" + b_zxbl);
        }
        if ("".equals(hblx))
            hblx = "CNY";//default
        sql = "select selectname from workflow_selectitem where fieldid=15381 and selectvalue=" + xzgjb;
        log.info("薪资岗级别sql语句：" + sql);
        rs.execute(sql);
        while (rs.next()) {
            xzgjb = rs.getString("selectname");//薪资岗级别
        }
        sql = "select selectname from workflow_selectitem where fieldid=15382 and selectvalue=" + xzgdc;
        log.info("薪资岗档次sql语句：" + sql);
        rs.execute(sql);
        while (rs.next()) {
            xzgdc = rs.getString("selectname");//薪资岗档次
        }
        //明细表
        sql = "select gzfl,je from " + tablename + "_dt5 where mainid=" + zbid;
        log.info("津贴福利sql语句：" + sql);
        rs.execute(sql);
        int count = 1;
        if (rs.getCounts() > 0)
            count = rs.getCounts();
        String[] gzfl = new String[count];//津贴类型
        String[] je = new String[count];//金额
        for (int i = 0; i < count; i++) {
            gzfl[i] = rs.getString("gzfl");
            je[i] = rs.getString("je");
        }
        //拼接xml
        StringBuffer xml = new StringBuffer("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">");
        xml.append("<soapenv:Header/>");
        xml.append("<soapenv:Body>");
        xml.append("<urn:Zhri074>");
        xml.append("<Return>");
        xml.append("<item>");
        xml.append("<Type></Type>");
        xml.append("<Id></Id>");
        xml.append("<Number></Number>");
        xml.append("<Message></Message>");
        xml.append("<LogNo></LogNo>");
        xml.append("<LogMsgNo></LogMsgNo>");
        xml.append("<MessageV1></MessageV1>");
        xml.append("<MessageV2></MessageV2>");
        xml.append("<MessageV3></MessageV3>");
        xml.append("<MessageV4></MessageV4>");
        xml.append("<Parameter></Parameter>");
        xml.append("<Row></Row>");
        xml.append("<Field></Field>");
        xml.append("<System></System>");
        xml.append("</item>");
        xml.append("</Return>");
        xml.append("<T0008>");
        xml.append("<item>");
        xml.append("<IPernr>" + hth + "</IPernr>");
        xml.append("<IBegda>" + sxrq + "</IBegda>");
        xml.append("<IWaers>" + hblx + "</IWaers>");
        xml.append("<IPreas>" + sqyy + "</IPreas>");
        xml.append("<ITrfar>" + xzlx + "</ITrfar>");
        xml.append("<ITrfgb>" + xzqy + "</ITrfgb>");
        xml.append("<ITrfgr>" + xzgjb + "</ITrfgr>");
        xml.append("<ITrfst>" + xzgdc + "</ITrfst>");
        xml.append("<IZgz8001>" + jbgz + "</IZgz8001>");
        xml.append("<IZgz8010>" + jxgz + "</IZgz8010>");
        xml.append("<IZgz8020>" + nzjjjs + "</IZgz8020>");
        xml.append("</item>");
        xml.append("</T0008>");
        //region[==新增字段==]
        xml.append("<T0014>");
        //地区系数(申请)存在值时
        if (b_dqxssq.compareTo(new BigDecimal("0.0")) == 1) {
            xml.append("<item>");
            xml.append("<Pernr>" + hth + "</Pernr>");
            xml.append("<Begda>" + sxrq + "</Begda>");
            xml.append("<Lgart>5025</Lgart>");
            xml.append("<Betrg></Betrg>");
            xml.append("<Anzhl>" + b_dqxssq + "</Anzhl>");
            xml.append("<Zeinh></Zeinh>");
            xml.append("<Preas>" + sqyy + "</Preas>");
            xml.append("</item>");
        }
        //行业只能系数(申请)存在值时：
        if (b_xyznxssq.compareTo(new BigDecimal("0.0")) == 1) {
            xml.append("<item>");
            xml.append("<Pernr>" + hth + "</Pernr>");
            xml.append("<Begda>" + sxrq + "</Begda>");
            xml.append("<Lgart>5100</Lgart>");
            xml.append("<Betrg></Betrg>");
            xml.append("<Anzhl>" + b_xyznxssq + "</Anzhl>");
            xml.append("<Zeinh></Zeinh>");
            xml.append("<Preas>" + sqyy + "</Preas>");
            xml.append("</item>");
        }
        //地区补贴(申请)不为空时
        if (b_dqbtsq.compareTo(new BigDecimal("0.0")) == 1) {
            xml.append("<item>");
            xml.append("<Pernr>" + hth + "</Pernr>");
            xml.append("<Begda>" + sxrq + "</Begda>");
            xml.append("<Lgart>5200</Lgart>");
            xml.append("<Betrg></Betrg>");
            xml.append("<Anzhl>" + b_dqbtsq + "</Anzhl>");
            xml.append("<Zeinh></Zeinh>");
            xml.append("<Preas>" + sqyy + "</Preas>");
            xml.append("</item>");
        }
        //试用期执行比例不为空时
        if (b_zxbl.compareTo(new BigDecimal("0.0")) == 1) {
            xml.append("<item>");
            xml.append("<Pernr>" + hth + "</Pernr>");
            xml.append("<Begda>" + sxrq + "</Begda>");
            xml.append("<Lgart>5080</Lgart>");
            xml.append("<Betrg></Betrg>");
            xml.append("<Anzhl>" + b_zxbl + "</Anzhl>");
            xml.append("<Zeinh></Zeinh>");
            xml.append("<Preas>" + sqyy + "</Preas>");
            xml.append("</item>");
        }
        xml.append("</T0014>");
        //endregion
        xml.append("</urn:Zhri074>");
        xml.append("</soapenv:Body>");
        xml.append("</soapenv:Envelope>");
        log.info("入职定薪XML:" + xml.toString());
        try {
            String dxUrl = bb.getPropValue("personnel-erdos", "rzdxurl");//获取配置文件信息
            SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
            String responseXml = salarRangeUtil.sendPost(dxUrl, xml.toString(), "utf-8");
            log.info("入职定薪接口-SAP返回消息:" + responseXml);//打印日志
            Document doc = null;//解析返回值开始
            try {
                doc = DocumentHelper.parseText(responseXml);
                Iterator<Element> its2 = doc.selectNodes("//Type").iterator();
                while (its2.hasNext()) {
                    Element element = its2.next();
                    fhz = element.getText();
                    if (!"".equals(fhz))
                        break;
                }

                //返回值失败时,则提示失败
                if ("E".equals(fhz)) {
                    Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
                    while (its3.hasNext()) {
                        Element element2 = its3.next();
                        strMessage = element2.getText();
                        if (!"".equals(strMessage)) {
                            break;
                        }
                    }

                }
            } catch (DocumentException e) {
                log.info("接口返回信息格式化异常，异常信息：" + e.toString());
                log.info("接口执行结束=======>");
                e.printStackTrace();
                return FAILURE_AND_CONTINUE;
            }//解析结束
        } catch (Exception e) {
            log.info("接口返回信息格式化异常，异常信息：" + e.toString());
            log.info("接口执行结束=======>");
            e.printStackTrace();
            return FAILURE_AND_CONTINUE;
        }
        if ("S".equals(fhz)) {
            log.info("成功！");
            log.info("接口执行结束=======>");
            return SUCCESS;
        } else {
            if (!"".equals(strMessage)) {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
                log.info("错误信息：" + strMessage);
            }
            log.info("入职定薪接口日志结束=======>");
            return FAILURE_AND_CONTINUE;
        }
    }

    private String getxzlx(String tj) {
        String jg = "";
        switch (tj) {
            case "0":
                jg = "10";
                break;
            default:
                jg = "";
        }
        return jg;
    }

    private String getxzqy(String tj) {
        String jg = "";
        switch (tj) {
            case "0":
                jg = "10";
                break;
            case "1":
                jg = "20";
                break;
            case "2":
                jg = "30";
                break;
            default:
                jg = "";
        }
        return jg;
    }
}
